package com.yy.bolg.controller.testing;

import com.yy.bolg.constant.ResultCode;
import com.yy.bolg.entity.TestingAccessoryBean;
import com.yy.bolg.entity.TestingBean;
import com.yy.bolg.entity.WorkOrderBean;
import com.yy.bolg.request.TestingAccessoryRequest;
import com.yy.bolg.request.TestingRequest;
import com.yy.bolg.resp.TestingResp;
import com.yy.bolg.service.TestingAccessoryService;
import com.yy.bolg.service.TestingService;
import com.yy.bolg.service.WorkOrderService;
import com.yy.bolg.util.CodeGenerateUtil;
import com.yy.bolg.util.RespenseJsonTransformationUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检测控制器
 * @author enmaoFu
 * @date 2024-11-12
 * RequestMapping("/testing") 是说明这个类的访问地址是 /testing
 */
@Api(tags = {"检测"})
@Controller
@RequestMapping(value = "/testing")
public class TestingController {

    @Autowired
    private TestingService testingService;// 自动载入Service对象
    @Autowired
    private TestingAccessoryService testingAccessoryService;// 自动载入Service对象
    @Autowired
    private WorkOrderService workOrderService;// 自动载入Service对象

    /**
     * 提交检测结果
     * 服务器访问路径：http://localhost:8080/bolg/testing/addTesting
     * 本地访问路径：http://localhost:8111/testing/addTesting
     * 参数：
     * {"customerCode":"CUS20241102102456","feedbackCode":"FEED20241102142035","workOrderCode":"WROR20241111222936","testingContent":"这个问题不大，换了一个开关就好了","testingVideoFile":"","testingVoiceFile":"","testingImgFile":"https://img2.baidu.com/it/u=3434434358,2368357664&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500,https://img2.baidu.com/it/u=3434434358,2368357664&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500","testingTotal":99.32,"tarList":[{"accessoryCode":"ACC20241104710750","accessoryName":"公牛开关","accessoryPrice":38.54,"accessoryClassCode":"ACCC20241104212801","accessoryClassName":"开关","testingAccessoryNumber":3},{"accessoryCode":"ACC20241104135685","accessoryName":"施耐德开关","accessoryPrice":58.54,"accessoryClassCode":"ACCC20241104212801","accessoryClassName":"开关","testingAccessoryNumber":3}]}
     */
    @ApiOperation(value = "提交检测", notes = "参数：{\"customerCode\":\"CUS20241102102456\",\"feedbackCode\":\"FEED20241102142035\",\"workOrderCode\":\"WROR20241111222936\",\"testingContent\":\"这个问题不大，换了一个开关就好了\",\"testingVideoFile\":\"\",\"testingVoiceFile\":\"\",\"testingImgFile\":\"https://img2.baidu.com/it/u=3434434358,2368357664&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500,https://img2.baidu.com/it/u=3434434358,2368357664&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500\",\"testingTotal\":99.32,\"tarList\":[{\"accessoryCode\":\"ACC20241104710750\",\"accessoryName\":\"公牛开关\",\"accessoryPrice\":38.54,\"accessoryClassCode\":\"ACCC20241104212801\",\"accessoryClassName\":\"开关\",\"testingAccessoryNumber\":3},{\"accessoryCode\":\"ACC20241104135685\",\"accessoryName\":\"施耐德开关\",\"accessoryPrice\":58.54,\"accessoryClassCode\":\"ACCC20241104212801\",\"accessoryClassName\":\"开关\",\"testingAccessoryNumber\":3}]}")
    @RequestMapping(value = "/addTesting",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String addTesting(@RequestBody TestingRequest request){
        String result;
        String testingCode = CodeGenerateUtil.getTestingCode();

        TestingBean testingBean = new TestingBean();
        testingBean.setPsTestingCode(testingCode);
        testingBean.setPsCustomerCode(request.getCustomerCode());
        testingBean.setPsFeedbackCode(request.getFeedbackCode());
        testingBean.setPsWorkOrderCode(request.getWorkOrderCode());
        testingBean.setPsTestingContent(request.getTestingContent());
        testingBean.setPsTestingVideoFile(request.getTestingVideoFile());
        testingBean.setPsTestingVoiceFile(request.getTestingVoiceFile());
        testingBean.setPsTestingImgFile(request.getTestingImgFile());
        testingBean.setPsTestingTotal(request.getTestingTotal());

        List<TestingAccessoryBean> testingAccessoryBeans = new ArrayList<>();
        for(TestingAccessoryRequest atr: request.getTarList()){
            TestingAccessoryBean testingAccessoryBean = new TestingAccessoryBean();
            testingAccessoryBean.setPsTestingCode(testingCode);
            testingAccessoryBean.setPsAccessoryCode(atr.getAccessoryCode());
            testingAccessoryBean.setPsAccessoryName(atr.getAccessoryName());
            testingAccessoryBean.setPsAccessoryPrice(atr.getAccessoryPrice());
            testingAccessoryBean.setPsAccessoryClassCode(atr.getAccessoryClassCode());
            testingAccessoryBean.setPsAccessoryClassName(atr.getAccessoryClassName());
            testingAccessoryBean.setPsTestingAccessoryNumber(atr.getTestingAccessoryNumber());
            testingAccessoryBeans.add(testingAccessoryBean);
        }

        WorkOrderBean workOrderBean = new WorkOrderBean();
        workOrderBean.setPsWorkOrderCode(request.getWorkOrderCode());
        workOrderBean.setPsTestingCode(testingCode);
        workOrderBean.setPsWorkOrderState(4);

        try {
            testingService.add(testingBean);

            for(TestingAccessoryBean tab: testingAccessoryBeans){
                testingAccessoryService.add(tab);
            }

            workOrderService.workAddTesting(workOrderBean);

            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "提交检测结果成功", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 检测完成
     * 服务器访问路径：http://localhost:8080/bolg/testing/workCompleteTesting
     * 本地访问路径：http://localhost:8111/testing/workCompleteTesting
     * 参数：
     * {"workOrderCode":"WROR20241111222936"}
     */
    @ApiOperation(value = "完成检测", notes = "参数：{\"workOrderCode\":\"WROR20241111222936\"}")
    @RequestMapping(value = "/workCompleteTesting",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String workCompleteTesting(@RequestBody TestingRequest request){
        String result;
        WorkOrderBean workOrderBean = new WorkOrderBean();
        workOrderBean.setPsWorkOrderCode(request.getWorkOrderCode());
        workOrderBean.setPsWorkOrderTestingState(1);

        try {
            workOrderService.workCompleteTesting(workOrderBean);

            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "检测完成", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 获取检测结果
     * 服务器访问路径：http://localhost:8080/bolg/testing/getTestingInfo
     * 本地访问路径：http://localhost:8111/testing/getTestingInfo
     * 参数：
     * {"testingCode":"TEST20241112095532"}
     */
    @ApiOperation(value = "获取检测结果", notes = "参数：{\"testingCode\":\"TEST20241112095532\"}", response = TestingResp.class)
    @RequestMapping(value = "/getTestingInfo",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String getTestingInfo(@RequestBody TestingRequest request){
        String result;

        TestingBean testingBean = new TestingBean();
        testingBean.setPsTestingCode(request.getTestingCode());

        TestingAccessoryBean testingAccessoryBean = new TestingAccessoryBean();
        testingAccessoryBean.setPsTestingCode(request.getTestingCode());

        try {
            TestingBean returnBean = testingService.findOneByCode(testingBean);
            if (null != returnBean){
                TestingResp resp = new TestingResp();
                resp.setTestingCode(returnBean.getPsTestingCode());
                resp.setCustomerCode(returnBean.getPsCustomerCode());
                resp.setFeedbackCode(returnBean.getPsFeedbackCode());
                resp.setWorkOrderCode(returnBean.getPsWorkOrderCode());
                resp.setTestingContent(returnBean.getPsTestingContent());
                resp.setTestingVideoFile(returnBean.getPsTestingVideoFile());
                resp.setTestingVoiceFile(returnBean.getPsTestingVoiceFile());
                resp.setTestingImgFile(returnBean.getPsTestingImgFile());
                resp.setTestingAgree(returnBean.getPsTestingAgree());
                resp.setTestingAgreeImg(returnBean.getPsTestingAgreeImg());
                resp.setTestingTotal(returnBean.getPsTestingTotal());

                List<TestingAccessoryBean> testingAccessoryBeans = testingAccessoryService.findAllByCondition(testingAccessoryBean);
                List<TestingAccessoryRequest> testingAccessoryRequests = new ArrayList<>();
                for(TestingAccessoryBean tabItem: testingAccessoryBeans){
                    TestingAccessoryRequest testingAccessoryRequest = new TestingAccessoryRequest();
                    testingAccessoryRequest.setTestingCode(tabItem.getPsTestingCode());
                    testingAccessoryRequest.setAccessoryCode(tabItem.getPsAccessoryCode());
                    testingAccessoryRequest.setAccessoryName(tabItem.getPsAccessoryName());
                    testingAccessoryRequest.setAccessoryPrice(tabItem.getPsAccessoryPrice());
                    testingAccessoryRequest.setAccessoryClassCode(tabItem.getPsAccessoryClassCode());
                    testingAccessoryRequest.setAccessoryClassName(tabItem.getPsAccessoryClassName());
                    testingAccessoryRequest.setTestingAccessoryNumber(tabItem.getPsTestingAccessoryNumber());
                    testingAccessoryRequests.add(testingAccessoryRequest);
                }
                resp.setTarList(testingAccessoryRequests);

                Map map = new HashMap();
                map.put("data", resp);
                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "获取检测结果成功", map);
            }else{
                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL,"暂无数据",new HashMap());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 审批检测提交的附件信息
     * 服务器访问路径：http://localhost:8080/bolg/testing/examineTestingAccessory
     * 本地访问路径：http://localhost:8111/testing/examineTestingAccessory
     * 参数：
     * {"testingCode":"TEST20241112095532","testingAgree":1,"testingAgreeImg":"https://img2.baidu.com/it/u=222462747,2327477984&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=800"}
     */
    @ApiOperation(value = "审批检测提交的附件信息", notes = "参数：{\"testingCode\":\"TEST20241112095532\",\"testingAgree\":1,\"testingAgreeImg\":\"https://img2.baidu.com/it/u=222462747,2327477984&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=800\"}")
    @RequestMapping(value = "/examineTestingAccessory",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String examineTestingAccessory(@RequestBody TestingRequest request){
        String result;

        TestingBean testingBean = new TestingBean();
        testingBean.setPsTestingCode(request.getTestingCode());
        testingBean.setPsTestingAgree(request.getTestingAgree());
        testingBean.setPsTestingAgreeImg(request.getTestingAgreeImg());

        try {
            testingService.examine(testingBean);

            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "审批成功", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }
}
